import { createRouter, createWebHashHistory } from 'vue-router'
import { useAuthStore } from '../stores/auth'
import Login from '../views/Login.vue'
import TodoList from '../components/TodoList.vue'
import Register from '../views/Register.vue'
import Images from '../views/Images.vue'

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'Login',
      component: Login
    },
    {
      path: '/register',
      name: 'Register',
      component: Register
    },
    {
      path: '/images',
      name: 'Images',
      component: Images
    },
    {
      path: '/todos',
      name: 'TodoList',
      component: TodoList,
      meta: { requiresAuth: true }
    },
    {
      path: '/',
      redirect: '/todos'
    }
  ]
})

// 路由守卫配置
router.beforeEach((to, from, next) => {
  const authStore = useAuthStore()
  const isAuthenticated = authStore.isAuthenticated
  
  if (to.meta.requiresAuth && !isAuthenticated) {
    next({ name: 'Login' })
  } else {
    next()
  }
})

export default router